/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if !defined(___GRAPHC2D_TEXT_WINDOW_H___)
#define ___GRAPHC2D_TEXT_WINDOW_H___

#if !defined(___GRAPH2D_STATIC_CONTAINER_H___)
#include "staticContainer.h"
#endif

#if !defined(___GRAPHC2D_TEXT_H___)
#include "text.h"
#endif

#if !defined(___GRAPH2D_WINDOW_H___)
#include "window.h"
#endif

#include <float.h>

namespace Graph2D
{
	class Text;

	typedef StaticContainer<1> TextWindowContainer;

	class TextWindow : public Window<TextWindowContainer>
	{
		DISALLOW_COPY_AND_ASSIGN(TextWindow);

		typedef Window<TextWindowContainer> super;

	public:
		TextWindow()
			: timer(0)
			, wait(FLT_MAX)
		{
			setType(TYPE_TEXT_WINDOW);
			set(0, text);
		}

		virtual ~TextWindow()
		{
		}

		virtual const char* getTypeName() const
		{
			return "TextWindow";
		}

		void clear()
		{
			text.clear();
		}

		bool empty() const
		{
			return text.empty();
		}

		void setFontName(const std::string& fontName)
		{
			text.setFontName(fontName);
		}

		void setFontSize(const float fontSize)
		{
			text.setFontSize(fontSize);
		}

		const Color& getTextColor() const
		{
			return text.getTextColor();
		}

		void setTextColor(const Color& color)
		{
			text.setTextColor(color);
		}

		void setTextColor(const float r, const float g, const float b, const float a)
		{
			setTextColor(Color(r, g, b, a));
		}

		TextAlignmentH getTextAlignmentH() const
		{
			return text.getTextAlignmentH();
		}

		void setTextAlignmentH(const TextAlignmentH textAlignment)
		{
			text.setTextAlignmentH(textAlignment);
		}

		TextAlignmentV getTextAlignmentV() const
		{
			return text.getTextAlignmentV();
		}

		void setTextAlignmentV(const TextAlignmentV textAlignment)
		{
			return text.setTextAlignmentV(textAlignment);
		}

		const std::string& getString() const
		{
			return text.getString();
		}

		void setString(const std::string& text)
		{
			this->text.setString(text);
			if(closed())
			{
				open();
			}
			timer = 0;
		}

		void format(const char* format, ...)
		{
			va_list ap;
			va_start(ap, format);
			vformat(format, ap);
			va_end(ap);
		}

		void vformat(const char* format, va_list ap)
		{
			text.vformat(format, ap);
			if(closed())
				open();
			timer = 0;
		}

		void replace(const char* key, const char* word)
		{
			text.replace(key, word);
		}

		float getAutoCloseTimer() const
		{
			return wait;
		}

		void setAutoCloseTimer(const float wait)
		{
			this->wait = wait;
		}

		void disableAutoClose()
		{
			wait = FLT_MAX;
		}

		bool finished() const
		{
			return (wait <= timer) || closed();
		}

		Text& getText()
		{
			return text;
		}

		const Text& getText() const
		{
			return text;
		}

#if defined(DEBUG) || defined(_DEBUG) || defined(_DEBUG_) || !defined(NDEBUG)
		virtual void open()
		{
			super::open();
		}
		virtual void close()
		{
			super::close();
		}
#endif

		virtual void onUpdate(const UpdateInfomation& updateInfomation)
		{
			if(isAnimationStoped() && !closed())
			{
				timer += updateInfomation.deltaTime;
				if(wait < timer)
				{
					close();
				}
			}
			super::onUpdate(updateInfomation);
		}

#if defined(DEBUG) || defined(_DEBUG) || defined(_DEBUG_) || !defined(NDEBUG)
		virtual bool onDraw(const DrawRect& drawRect)
		{
			return super::onDraw(drawRect);
		}
#endif
		virtual void onSizeChanged()
		{
			text.setPosition(0, 0);
			text.setSize(getClientSize());
		}

	private:
		Text text;
		float timer;
		float wait;
	};
}

#endif
